﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// PaperMatch 的摘要说明
/// </summary>

public class MatchValue
{
    public int DoctorGuid;
    public decimal Value;
    public decimal Append;
    public int PaperNum;

    public MatchValue(ref ArrayList pks, int dg,ref Paper pap)
    {
        this.DoctorGuid = dg;
        //string sql = "select Yjfx as n from Doctor where UserGuid=" + dg.ToString();
        Doctor doc = new Doctor(dg);
        string dkw = doc.Yjfx;
        ArrayList dks = PaperMatch.TakeKeywords(dkw);
        decimal[] pkwmv = new decimal[pks.Count];
        int[] pkwmi = new int[pks.Count];
        //decimal pkwm = 0;
        for (int i = 0; i < pks.Count; i++)
        {
            pkwmi[i] = -1;
            pkwmv[i] = 0;
        }
        decimal pkwmv_sum = 0;
        for (int i = 0; i < pks.Count; i++)
        {
            string pk = pks[i].ToString();
            int index = -1;
            for (int t = 0; t < dks.Count; t++)
            {
                string dk = dks[t].ToString();
                decimal v = PaperMatch.MatchString(pk, dk);
                if (v > pkwmv[i])
                {
                    pkwmv[i] = v;
                    index = i;
                }
            }
            if (Fun.ArrayContain(pkwmi, index) && index != -1)
            {
                dks.Add(dks[i].ToString());
                index = dks.Count - 1;
            }
            pkwmi[i] = index;
            pkwmv_sum += pkwmv[i];
        }
        this.Value = pkwmv_sum / ((decimal)pkwmv.Length);
        if (pap.CategoryGuid == doc.CategoryGuid)
        {
            this.Value = this.Value + Convert.ToDecimal(Math.Sqrt(1-Convert.ToDouble(this.Value)));
        }
        this.Append = pkwmv_sum / (Convert.ToDecimal(pks.Count) + Convert.ToDecimal(dks.Count) - pkwmv_sum);
        this.DoctorGuid = dg;
        //return mv;
    }
}

public class MatchValueComparer : IComparer
{
    int IComparer.Compare(object x, object y)
    {
        MatchValue mvx = (MatchValue)x;
        MatchValue mvy = (MatchValue)y;
        if (mvx.Value != mvy.Value)
        {
            return mvx.Value.CompareTo(mvy.Value);
        }
        else
        {
            return mvx.Append.CompareTo(mvy.Append);
        }
    }
}

public class PaperMatch
{

    public int StudentGuid;
	public PaperMatch(int _StudentGuid)
	{
        this.StudentGuid = _StudentGuid;
	}

    public static ArrayList TakeKeywords(string kw)
    {
        ArrayList ary = new ArrayList();
        string[] ss = kw.Split();
        foreach (string s in ss)
        {
            string k = s.ToLower();
            if (k != "")
            {
                ary.Add(k);
            }
        }
        return ary;
    }

    public static decimal MatchString(string s1, string s2)
    {
        if (s1 == s2)
            return 1;
        else if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2))
            return 0;
        int[,] d = new int[s1.Length, s2.Length];

        int index = 0;
        int length = 0;

        for (int i = 0; i < s1.Length; i++)
        {
            for (int j = 0; j < s2.Length; j++)
            {
                // 左上角值
                int n = i - 1 >= 0 && j - 1 >= 0 ? d[i - 1, j - 1] : 0;

                // 当前节点值 = "1 + 左上角值" : "0"
                d[i, j] = s1[i] == s2[j] ? 1 + n : 0;

                // 如果是最大值，则记录该值和行号
                if (d[i, j] > length)
                {
                    length = d[i, j];
                    index = i;
                }
            }
        }
        if (length < 2)
        {
            return 0;
        }
        decimal v = ((decimal)length) / Convert.ToDecimal(s1.Length + s2.Length - length);
        return v;
       // return s1.Substring(index - length + 1, length);
    }

    public void StartMatch()
    {
        Student stu = new Student(this.StudentGuid);
        Paper pap = new Paper(this.StudentGuid);
        string pkw = pap.Keywords;
        ArrayList pks = TakeKeywords(pkw);
        int TeamGuid = stu.TeamGuid;
        string sql = "select distinct u.Guid from Users u Join Doctor d on u.Guid=d.UserGuid where d.Expert=1 and d.TeamGuid !="+TeamGuid.ToString();
        DataTable dt = Fun.CreateDt(sql);
        ArrayList mvs = new ArrayList();
        MatchValue mv;
        foreach (DataRow dr in dt.Rows)
        {
            mv = new MatchValue(ref pks, Convert.ToInt32(dr[0]),ref pap);
            if (mv.Value != 0)
            {
                mvs.Add(mv);
            }
        }
        MatchValueComparer mvc = new MatchValueComparer();
        mvs.Sort(mvc);
        int DoctorGuid = 0;
        for (int i = mvs.Count-1; i>=0; i--)
        {
            mv = (MatchValue)mvs[i];
            if (mv.PaperNum < Params.MaxPaperNum)
            {
                DoctorGuid = mv.DoctorGuid;
                break;
            }
        }
        if (DoctorGuid == 0)
        {
            sql = "select top 1 UserGuid from Doctor where Expert=1 and TeamGuid !="+TeamGuid.ToString()+" order by PaperNum asc,NEWID() desc";
            dt = Fun.CreateDt(sql);
            if (dt.Rows.Count > 0)
            {
                DoctorGuid = Convert.ToInt32(dt.Rows[0][0]);
            }
        }
        if (DoctorGuid != 0)
        {
            sql = "update Student set ExpertGuid="+DoctorGuid.ToString()+" where UserGuid="+stu.Guid.ToString();
            Fun.Execute(sql);
        }
    }


    //public static MatchValue MatchKeywords(string kw1, string kw2)
    //{
    //    return mv;
    //}
}
